package com.ocn.li.scmdy.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ocn.li.scmdy.common.utils.*;
import com.ocn.li.scmdy.common.validator.ValidatorUtils;
import com.ocn.li.scmdy.modules.app.entity.AccountRelationAttribute;
import com.ocn.li.scmdy.modules.app.entity.Accounts;
import com.ocn.li.scmdy.modules.app.form.MobileLoginForm;
import com.ocn.li.scmdy.modules.app.service.LoginService;
import com.ocn.li.scmdy.modules.app.service.RegionService;
import com.ocn.li.scmdy.modules.cache.DyConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName ApiLoginController
 * @Description TODO
 * @Author Li9527
 * @Date 2020/9/9 11:32
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/mobile/login")
@Api(value = "AppLoginController", tags = {"移动端--登录注册"})
public class ApiLoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    RegionService regionService;

    /**
     * @Author Li9527
     * @Description  重写的登陆接口
     * @Date  13:43
     * @Param [form]
     * @return com.ocn.li.scmdy.common.utils.R
     **/
    @PostMapping("/v1")
    @ApiOperation("重写的登陆接口")
    public R login(@RequestBody MobileLoginForm form) {
        ValidatorUtils.validateEntity(form);
        System.out.println("移动端登录：userName=" + form.getUserName());
        Map<String,Object> map = new HashMap<>();
        Accounts account = loginService.selectOne(new EntityWrapper<Accounts>().eq(
                "ACCOUNT_NAME", form.getUserName()).eq("del_Flag", 0));
        if (account == null) {
            return R.error("无此用户");
        } else {
            Boolean flag = Encrypt.checkpass(form.getPassWord(), account.getPassword());
            if (!flag) {
                return R.error("密码错误");
            } else {
                // 将来要改写。直接jedis取/redis工具类也是多余
                Map<Object, Object> map1 = redisUtils.getMap(DyConstant.RedisKey.ACCOUNT_ID_LIST);
                Map<String, AccountRelationAttribute> acountListMap = new HashMap<>();
                if (map1.size() > 0) {
                    Iterator<Map.Entry<Object, Object>> entries = map1.entrySet().iterator();
                    while (entries.hasNext()) {
                        Map.Entry<Object, Object> entry = entries.next();
                        JSONObject jsonObject = JSONObject.parseObject(entry.getValue().toString());
                        if (jsonObject != null) {
                            AccountRelationAttribute attribute = jsonObject.toJavaObject(AccountRelationAttribute.class);
                            acountListMap.put(entry.getKey().toString(), attribute);
                        }
                    }
                }
                AccountRelationAttribute accu = acountListMap.get(account.getAccountName());
                //20190419     陈磊 有时候网格名称为空  设置网格名称
                if (!StringUtils.isBlank(accu.getDataAreaCode())) {
                    List<Object> attrList = new ArrayList<Object>();
//                    Region region = regionService.selectOne(new EntityWrapper<Region>().eq("region_code",accu.getDataAreaCode()));
                    accu.setAdministrativeDivision("测试一下");
                }
                map.put("accu",accu);
                map.put("name",accu.getUserName());
            }
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sf.format(new Date());

        if (account != null) {
            String tokenStr = FjqJwtUtil.createJWT(account.getAccountName(), account.getAccountName(), "1", account.getId(), account.getDataAreaCode());
            map.put("token",tokenStr);
        }
        return R.ok("检验成功").put("data",map);

    }

    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 11207);
        jedis.auth("123456");
        Map<String, AccountRelationAttribute> accountIdListMap = new HashMap<>();
        Map<String, String> map = jedis.hgetAll("account_id_list");
        Map<Object,Object> newMap = new HashMap<>();
        newMap.putAll(map);
        if (map.size() > 0) {
            Iterator<Map.Entry<Object, Object>> entries = newMap.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<Object, Object> entry = entries.next();
                JSONObject jsonObject = JSONObject.parseObject(entry.getValue().toString());
                if (jsonObject != null) {
                    AccountRelationAttribute attribute = jsonObject.toJavaObject(AccountRelationAttribute.class);
                    accountIdListMap.put(entry.getKey().toString(), attribute);
                }
            }
        }
        AccountRelationAttribute accu = accountIdListMap.get("tc13");
        System.out.println(accu.toString());
    }


}
